<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

class InterestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $ss=DB::table('product')->inRandomOrder()->take(6)->get()->all();
         $type=DB::table('type')->get()->all();
    	 $links=DB::table('links')->get();
        $user_id = session('homeuser')->id;
        $where = [];
        $db = DB::table('interest')->leftjoin('myorder','interest.product_id', '=', 'myorder.product_id')->leftjoin('product','product.id', '=', 'interest.product_id')->where('interest.user_id',$user_id);
         if($request->has('name')){
            // 获取要搜索的name字段的值
            $name = $request->input('name');
            //添加where模糊匹配条件
            $db->where('name','like',"%{$name}%");
            $where['name'] = $name;
        }
        // 分页
        $list = $db->paginate(3);
         return view('home.interest',['list'=>$list,'where'=>$where,'links'=>$links,'search'=>$ss,
         'all'=>$type]);
    }
     

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       
       
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {   

       
           
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id){
        $m = DB::table('interest')->where('product_id',$id)->delete();
        if($m>0){
            return redirect('home/interest');
        }else{
            return "删除失败";      
        }
    }
}
